//
//  HLVistorView.swift
//  HLwWeibo
//
//  Created by hqhxu on 16/2/27.
//  Copyright © 2016年 hqhxu. All rights reserved.
//

import UIKit

protocol HLVistorViewDelegate:NSObjectProtocol {
    
    func vistorDidClickRegistButton()
    func vistorDidClickLoginButton()
}

class HLVistorView: UIView {
    
    
    //设置代理属性
    weak var vistorDelegate:HLVistorViewDelegate?

    
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
        self.backgroundColor = UIColor(white: 237/255.0, alpha: 1)
            
        prepareUI()
    }

    required init?(coder aDecoder: NSCoder) {
        
        super.init(coder: aDecoder)
    }
    
    // MARK: - 添加子控件
    func prepareUI(){
        addSubview(iconView)
        //添加遮盖view
        addSubview(coverView)
        
        addSubview(homeView)
        addSubview(messageLable)
        addSubview(registButton)
        addSubview(loginButton)
        
        
        // 使用第三方框架不需要手动在外面设置 translatesAutoresizingMaskIntoConstraints = false
        
        //添加约束  约束统一加到父控件上
        //iconView
        iconView.snp_makeConstraints { (make) -> Void in
            // 如果是equalto父控件那么久可以不写   
            // 如果是父控件那么久可以不写view
            // 如果与是相对应的属性 那么就可以不写后面view的属性
            make.centerX.equalTo(snp_centerX)
            make.centerY.equalTo(snp_centerY).offset(-40)
        }
        
        //homeview
        homeView.snp_makeConstraints { (make) -> Void in
//            make.center.equalTo(iconView.center)
            make.centerX.equalTo(iconView.snp_centerX)
            make.centerY.equalTo(iconView.snp_centerY)
        }
        
        
        //messageLable
        messageLable.snp_makeConstraints { (make) -> Void in
            make.width.equalTo(240)
            make.centerX.equalTo(iconView.snp_centerX)
            make.top.equalTo(iconView.snp_bottom).offset(16)
        }
        
        
        //registButton
        registButton.snp_makeConstraints { (make) -> Void in
            
            make.leading.equalTo(messageLable.snp_leading)
            make.top.equalTo(messageLable.snp_bottom).offset(16)
            make.size.equalTo(CGSize(width: 100, height: 35))
        }
        
        //loginButton
        loginButton.snp_makeConstraints { (make) -> Void in
            make.trailing.equalTo(messageLable.snp_trailing)
            
            make.top.equalTo(registButton.snp_top)
            
            make.width.equalTo(registButton.snp_width)
            make.height.equalTo(registButton.snp_height)
        }

        // coverView
        coverView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(UIEdgeInsetsZero)
        }
    }
    
    // MARK: - 给iconview添加动画
    func animationWithIconView(){
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        
        animation.toValue = 2 * M_PI
        animation.duration = 20
        animation.repeatCount = MAXFLOAT
        
        //有一个属性设置为是否移除
        animation.removedOnCompletion = false
        
        
        iconView.layer.addAnimation(animation, forKey: nil)
        
    }
    
    // MARK: - 按钮点击事件
    func didClickRegistButton(){
        //点击按钮 要跳出一个控制器来
        vistorDelegate?.vistorDidClickRegistButton()
    }
    
    func didClickLoginButton() {
        vistorDelegate?.vistorDidClickLoginButton()
    }
    
    // MARK: - 懒加载
    
    lazy var coverView:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    
    lazy var iconView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    
    lazy var homeView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    
    lazy var messageLable: UILabel = {
        
        let lable = UILabel()
        
        lable.numberOfLines = 0
        lable.textAlignment = NSTextAlignment.Center
        lable.font = UIFont.systemFontOfSize(18)
        lable.textColor = UIColor.darkGrayColor()
        lable.text = "关注一些人,看看上面有惊喜"
        
        return lable
    }()
    
    
    lazy var registButton: UIButton = {
        
        let button = UIButton()
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        button.setTitle("注册", forState: UIControlState.Normal)
        button.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        
        button.addTarget(self, action: "didClickRegistButton", forControlEvents: UIControlEvents.TouchUpInside)
        
        return button
    }()
    
    
    lazy var loginButton: UIButton = {
        
        let button = UIButton()
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        button.setTitle("登录", forState: UIControlState.Normal)
        button.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)
        
        button.addTarget(self, action: "didClickLoginButton", forControlEvents: UIControlEvents.TouchUpInside)
        
        return button
    }()
    
    
}
